<template>
  <el-dialog :title="title" :visible.sync="dialogFormVisible" width="500px" @close="close">
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <!-- prop 是回显数据   form.username 是请求数据 -->

      <el-form-item label="头像" prop="fileList">
        <Upload :list.sync="form.fileList" :limit="1"></Upload>
      </el-form-item>

      <el-form-item label="用户名" prop="userName">
        <el-input v-model.trim="form.userName" autocomplete="off" />
      </el-form-item>

      <el-form-item label="登录账号" prop="loginName">
        <el-input v-model.trim="form.loginName" autocomplete="off" />
      </el-form-item>

      <el-form-item label="手机号码" prop="phoneNumber">
        <el-input v-model.trim="form.phoneNumber" autocomplete="off" />
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input v-model.trim="form.password" autocomplete="off" :disabled="isDisabled" type="password" />
      </el-form-item>

      <el-form-item label="性别" prop="sex">
        <el-radio-group v-model="form.sex">
          <el-radio :label="'0'">男</el-radio>
          <el-radio :label="'1'">女</el-radio>
          <el-radio :label="'2'">其他</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="用户渠道" prop="channel">
        <el-radio-group v-model="form.channel">
          <el-radio :label="'0'">后台用户</el-radio>
          <el-radio :label="'1'">前台用户</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="用户类别" prop="houseUserType">
        <el-radio-group v-model="form.houseUserType">
          <el-radio :label="'0'">房东</el-radio>
          <el-radio :label="'1'">租客</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="账号状态" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio :label="'0'">启用</el-radio>
          <el-radio :label="'1'">停用</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="权限" prop="permissions">
        <el-checkbox-group v-model="form.permissions">
          <el-checkbox v-for="role in roles" :key="role.id" :label="role.id" :value="role.id">
            {{ role.roleName }}
          </el-checkbox>
        </el-checkbox-group>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { doEdit } from '@/api/userManagement'
  import { getRoleList } from '@/api/roleManagement'
  import Upload from '@/components/Upload.vue'

  export default {
    name: 'UserManagementEdit',
    components: {
      Upload,
    },
    data() {
      return {
        form: {
          userName: '',
          sex: '',
          loginName: '',
          phoneNumber: '',
          password: '',
          channel: '',
          houseUserType: '',
          status: '',
          fileList: [],
          permissions: [],
        },
        roles: [], // 角色列表数据将在获取后填充到这个数组中
        rules: {
          userName: [{ required: true, trigger: 'blur', message: '请输入用户名' }],
          // loginName: [{ required: true, trigger: 'blur', message: '请输入登录名称' }],
          phoneNumber: [{ required: true, trigger: 'blur', message: '请输入手机号码' }],
          password: [{ required: true, trigger: 'blur', message: '请输入密码' }],
          channel: [{ required: true, trigger: 'blur', message: '请输入用户渠道' }],
          status: [{ required: true, trigger: 'blur', message: '请输入账号状态' }],
          permissions: [{ required: true, trigger: 'blur', message: '请选择权限' }],
        },
        title: '',
        isDisabled: false,
        dialogFormVisible: false,
      }
    },
    created() {
      getRoleList({
        /* your data object */
      }).then((response) => {
        const date = response.data // 假设响应的数据在 response.data 中
        console.log('Returned data:', date)
        // 在这里可以对 date 进行处理或者赋值给组件的数据
        this.roles = date.list // 假设返回的数据结构包含 roles 字段，是一个数组
      })
    },
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '添加'
          this.isDisabled = false
        } else {
          this.isDisabled = true
          this.title = '编辑'
          this.form = Object.assign({}, row)
        }

        console.info('row  ----->  ', row)
        console.info('this.form  ----->  ', this.form)
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const parms = {
              ...this.form,
            }
            if (this.form.fileList != null) {
              parms.avatar = this.form.fileList[0]?.url
              parms.avatarId = this.form.fileList[0]?.id
            }
            const { msg } = await doEdit(parms)
            this.$baseMessage(msg, 'success')
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
    computed: {},
  }
</script>
